<?php

namespace app\model\v1\recovery;

use app\model\user\User;
use crmeb\traits\ModelTrait;
use think\Model;

class RecoveryUserRelation extends Model
{
    use ModelTrait;


    protected $updateTime = false;
    protected $createTime = false;

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid');
    }

    public function product()
    {
        return $this->hasOne(RecoveryProduct::class, 'id', 'product_id');
    }


    /**
     * 用户添加收藏
     * @param $productId
     * @param $uid
     * @param $is_collect
     * @return bool|int|string
     */
    public static function collect($productId='', $uid='', $is_collect='', $type='')
    {
        if($is_collect == 1){
            $data = [
                'product_id' => $productId,
                'type'       => $type,
                'uid'        => $uid
            ];
            //添加用户新增的商品收藏信息
            return self::insert($data);
        }else{
            $data = [
                'product_id' => $productId,
                'type'       => $type,
                'uid'        => $uid
            ];
            return self::where($data)->delete();  //将用户添加的收藏商品信息删除
        }
    }


    /**
     * 个人中心 - 我的收藏商品列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getCollectList($param = [])
    {
        //根据用户id， 获取收藏商品列表中的用户的收藏商品列表
        $data["data"] = self::alias("a")
            ->field("a.id,a.uid,
            b.id as product_id,b.product_name,b.dx,b.price,b.category_id,b.visit_count,b.is_sold,b.images,b.address,b.on_sale,b.is_sale,b.pre_sale_date,b.uid as shop_id,
            c.nickname,c.avatar")
            ->join('recovery_product b', 'a.product_id=b.id','left')
            ->join('user c', 'b.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->where(["a.uid"=> $param["uid"], "a.type"=>'collect']);
                //根据店铺(商品)名称搜索
                if(!empty($param["product_name"])) $query->where("b.product_name", 'like', '%'.$param["product_name"].'%');
                //根据在售状态搜索      2获取在售
                if(!empty($param["is_sale"])){
                    ////$query->where("b.is_sale", $param["is_sale"]);
                    if($param["is_sale"] == 1){     //1获取预售
                        $query->where("b.pre_sale_date", ">=", date("Y-m-d"));
                    }
                    if($param["is_sale"] == 2){     //2获取在售
                        $query->where("b.is_sale", 2)->whereOr(function ($query2) {
                            $query2->whereNull("b.pre_sale_date")->where("b.pre_sale_date", "<", date("Y-m-d"));
                        });
                    }
                }
            })
            ->order(['a.id' => 'desc'])
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::alias("a")
            ->field("a.id,a.uid,
            b.id as product_id,b.product_name,b.dx,b.price,b.category_id,b.visit_count,b.images,b.address,b.is_sale,b.pre_sale_date,b.uid as shop_id,
            c.nickname,c.avatar")
            ->join('recovery_product b', 'a.product_id=b.id','left')
            ->join('user c', 'b.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->where(["a.uid"=> $param["uid"], "a.type"=>'collect']);
                //根据店铺(商品)名称搜索
                if(!empty($param["product_name"])) $query->where("b.product_name", 'like', '%'.$param["product_name"].'%');
                //根据在售状态搜索
                if(!empty($param["is_sale"])){
                    ////$query->where("b.is_sale", $param["is_sale"]);
                    if($param["is_sale"] == 1){     //1获取预售
                        $query->where("b.pre_sale_date", ">=", date("Y-m-d"));
                    }
                    if($param["is_sale"] == 2){     //2获取在售
                        $query->where("b.is_sale", 2)->whereOr(function ($query2) {
                            $query2->whereNull("b.pre_sale_date")->where("b.pre_sale_date", "<", date("Y-m-d"));
                        });
                    }
                }
            })
            ->count();

        if(!empty($data["data"])){
            foreach ($data['data'] as $key => &$val) {
                /*$val["user"] = (object)[
                    "uid" => $val['uid'],
                    "nickname" => $val['nickname'],
                    "avatar" => $val['avatar'],
                ]; */
                $val["user"] = User::where("uid", $val["shop_id"])->field("uid,nickname,avatar")->find() ?? [];

                if(!empty($param["user_dx"])) {
                    $yx = explode(',', $param["user_dx"]);
                    $val['distance'] = RecoveryProduct::get_distance(explode(",", $val["dx"]), $yx);
                }
            }
        }

        return $data;
    }


    /**
     * 我收藏的 - count
     * @param $param
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getCollectListCount($param = [])
    {
        //接收用发送过来的参数，获取指定列表的数量count
        return self::alias("a")
            ->field("a.id,a.uid,b.id as product_id,b.product_name,b.price,b.category_id,b.visit_count,b.images,b.address,c.nickname,c.avatar")
            ->join('recovery_product b', 'a.product_id=b.id','left')
            ->join('user c', 'a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->where(["a.uid"=> $param["user_id"], "a.type"=>'collect']);
            })
            ->count();
    }



}